﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Web.UI.WebControls;
using NetDon.OA.Data;
using NetDon.OA.Data.CollectionClasses;
using NetDon.OA.Data.DaoClasses;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Data.HelperClasses;
using NetDon.OA.Data.Linq;
using NetDon.OA.Data.RelationClasses;
using NetDon.OA.Interface;
using NetDon.OA.Interface.CRM;
using NetDon.OA.Interface.eLogistics;
using SD.LLBLGen.Pro.ORMSupportClasses;

namespace NetDon.OA.Business
{
    public partial class RevenueMachineAccountService : IRevenueMachineAccountService
    {
        public int GetRevenueMachineAccountsCount(RevenueMachineAccountSearchCriteria criteria)
        {
            var collection = new RevenueMachineAccountCollection();
            return int.Parse(collection.GetScalar(RevenueMachineAccountFieldIndex.Id, null, AggregateFunction.CountDistinct, GetRevenueMachineAccountPredicate(criteria), GetRevenueMachineAccountSearchRelations(), null).ToString());


            return collection.GetDbCount(GetRevenueMachineAccountPredicate(criteria), GetRevenueMachineAccountSearchRelations());
        }

        private IRelationCollection GetRevenueMachineAccountSearchRelations()
        {
            var relation = new RelationCollection(RevenueMachineAccountEntity.Relations.IssuingOrderEntityUsingIssuingOrderId, JoinHint.Left);
            relation.Add(IssuingOrderEntity.Relations.IssuingOrderReceiptInfoTrackEntityUsingIssuingOrderId, JoinHint.Left);

            relation.Add(new DynamicRelation(EntityType.RevenueMachineAccountEntity, JoinHint.Left,
                                             EntityType.CompanyEntity, string.Empty, string.Empty,
                                             CompanyFields.CompanyId == RevenueMachineAccountFields.AreaId));
            relation.Add(new DynamicRelation(EntityType.RevenueMachineAccountEntity, JoinHint.Left, EntityType.UserEntity, string.Empty, string.Empty,
                                             RevenueMachineAccountFields.LastUpdatedBy == UserFields.UserId));
            return relation;
        }

        public DataTable SearchRevenueMachineAccountDataByCondition(RevenueMachineAccountSearchCriteria criteria, string sortExpression, System.Web.UI.WebControls.SortDirection sortDirection, int pageSize, int pageIndex)
        {

            var fields = new EntityFields(29);
            var index = 0;

            //0
            fields[index++] = new EntityField("Revenues", new ScalarQueryExpression(new EntityField("TotalDeliveryRevenue",
                                              new Expression(
                                                   IssuingOrderDeliveryFields.ConvertedOrderVolumnWeight,
                                                  ExOp.Mul, IssuingOrderDeliveryFields.RevenueUnitCost), AggregateFunction.Sum),
                                                                  IssuingOrderFields.Id ==
                                                                  IssuingOrderDeliveryFields.IssuingOrderId & IssuingOrderFields.Id == RevenueMachineAccountFields.IssuingOrderId, new RelationCollection(IssuingOrderEntity.Relations.IssuingOrderDeliveryEntityUsingIssuingOrderId)));


            //1
            fields[index++] = new EntityField("UnloadingCostWithFactory", new ScalarQueryExpression(new EntityField("TotalUnloadingCostWithFactory",
                                     new Expression(
                                        IssuingOrderDeliveryFields.ConvertedOrderVolumnWeight,
                                         ExOp.Mul, IssuingOrderDeliveryFields.GoodsUnloadingPriceRatio), AggregateFunction.Sum),
                                                         IssuingOrderFields.Id ==
                                                         IssuingOrderDeliveryFields.IssuingOrderId & IssuingOrderFields.Id == RevenueMachineAccountFields.IssuingOrderId, new RelationCollection(IssuingOrderEntity.Relations.IssuingOrderDeliveryEntityUsingIssuingOrderId)));

            //2
            fields[index++] = new EntityField("TotalRevenues", new Expression(new Expression(
           new Expression(new Expression(
         fields[1],
           ExOp.Add, RevenueMachineAccountFields.CarSharingCostWithFactory), ExOp.Add, RevenueMachineAccountFields.OtherRevenue), ExOp.Add, fields[0]), ExOp.Add, RevenueMachineAccountFields.Subsidy));


            //3
            fields[index++] = new EntityField("UnitCost",
                                              new Expression(IssuingOrderFields.ShuntingCost, ExOp.Div, new ScalarQueryExpression(  IssuingOrderDeliveryFields.ConvertedOrderVolumnWeight.SetAggregateFunction(AggregateFunction.Sum)
                                                , IssuingOrderFields.Id ==
                                                                  IssuingOrderDeliveryFields.IssuingOrderId, new RelationCollection(IssuingOrderEntity.Relations.IssuingOrderDeliveryEntityUsingIssuingOrderId))));



            //4
            fields[index++] = new EntityField("Cost",
             new Expression(new Expression(IssuingOrderFields.ShuntingCost, ExOp.Add, IssuingOrderFields.GoodsUnloadingCost), ExOp.Add, new ScalarQueryExpression(IssuingOrderVehicleInfoFields.CarSharingPrice.SetAggregateFunction(AggregateFunction.Sum),
                       IssuingOrderFields.Id == IssuingOrderVehicleInfoFields.IssuingOrderId)));
            //5
            fields[index++] = new EntityField("ExtraCost", new Expression(new ScalarQueryExpression(MachineAccountPaymentFields.OtherAmountFromManufactor.SetAggregateFunction(AggregateFunction.Sum), IssuingOrderFields.Id == MachineAccountPaymentFields.IssuingOrderId), ExOp.Sub, new ScalarQueryExpression(MachineAccountPaymentFields.DeductionExceptionsAmount.SetAggregateFunction(AggregateFunction.Sum), IssuingOrderFields.Id == MachineAccountPaymentFields.IssuingOrderId)));

            //6
            fields[index++] = new EntityField("Profit", new Expression(fields[2], ExOp.Sub, new Expression(fields[4], ExOp.Add, fields[5])));

            //7
            fields[index++] = new EntityField("ProfitRate", new Expression(fields[6]) * 100 / new Expression(fields[2]));


            fields[index++] = RevenueMachineAccountFields.Id;
            fields[index++] = RevenueMachineAccountFields.TransportationPlanCode;
            fields[index++] = RevenueMachineAccountFields.OrderCode;
            fields[index++] = IssuingOrderFields.ActualShippingDate;
            fields[index++] = IssuingOrderFields.ReceivingOrderDate;
            fields[index++] = RevenueMachineAccountFields.DueInvoiceDate;
            fields[index++] = RevenueMachineAccountFields.AccountsReceivableDate;
            fields[index++] = RevenueMachineAccountFields.IsStatementofAccountCompleted;
            fields[index++] = RevenueMachineAccountFields.IsInvoiced;
            fields[index++] = RevenueMachineAccountFields.ConfirmInvoiceDate;
            fields[index++] = RevenueMachineAccountFields.IsAccountsReceivable;
            fields[index++] = RevenueMachineAccountFields.ConfirmAccountsReceiptDate;
            fields[index++] = RevenueMachineAccountFields.ConfirmStatementofAccountDate;

            fields[index++] = IssuingOrderFields.ActualEntruckingBoxAmount;
            fields[index++] = RevenueMachineAccountFields.IssuingOrderId;
            fields[index++] = RevenueMachineAccountFields.OtherRevenue;
            fields[index++] = RevenueMachineAccountFields.LastUpdatedDate;
            fields[index++] = RevenueMachineAccountFields.LastUpdatedBy;
            fields[index++] = RevenueMachineAccountFields.Exceptions;

            fields[index++] = CompanyFields.CompanyName;
            fields[index++] = UserFields.UserName;

            IEntityField sort = null;
            switch (sortExpression)
            {
                case "TransportationPlanCode":
                    sort = RevenueMachineAccountFields.TransportationPlanCode;
                    break;
                case "OrderCode":
                    sort = RevenueMachineAccountFields.OrderCode;
                    break;
                case "ActualShippingDate":
                    sort = IssuingOrderFields.ActualShippingDate;
                    break;
                case "ReceivingOrderDate":
                    sort = IssuingOrderFields.ReceivingOrderDate;
                    break;
                case "TotalRevenues":
                    sort = fields[2];
                    break;
                case "Revenues":
                    sort = fields[0];
                    break;
                case "UnloadingCostWithFactory":
                    sort = fields[1];
                    break;
                //case "UnitCost":
                //    sort = fields[3];
                //    break;
                case "Cost":
                    sort = fields[4];
                    break;
                case "ExtraCost":
                    sort = fields[5];
                    break;

                case "Profit":
                    sort = fields[6];
                    break;
                case "ProfitRate":
                    sort = fields[7];
                    break;

                case "DueInvoiceDate":
                    sort = RevenueMachineAccountFields.DueInvoiceDate;
                    break;

                case "AccountsReceivableDate":
                    sort = RevenueMachineAccountFields.AccountsReceivableDate;
                    break;

                case "IsStatementofAccountCompleted":
                    sort = RevenueMachineAccountFields.IsStatementofAccountCompleted;
                    break;
                case "IsInvoiced":
                    sort = RevenueMachineAccountFields.IsInvoiced;
                    break;
                case "ConfirmInvoiceDate":
                    sort = RevenueMachineAccountFields.ConfirmInvoiceDate;
                    break;
                case "IsAccountsReceivable":
                    sort = RevenueMachineAccountFields.IsAccountsReceivable;
                    break;
                case "ConfirmAccountsReceiptDate":
                    sort = RevenueMachineAccountFields.ConfirmAccountsReceiptDate;
                    break;
                case "ConfirmStatementofAccountDate":
                    sort = RevenueMachineAccountFields.ConfirmStatementofAccountDate;
                    break;

                case "ActualEntruckingBoxAmount":
                    sort = IssuingOrderFields.ActualEntruckingBoxAmount;
                    break;

                case "OtherRevenue":
                    sort = RevenueMachineAccountFields.OtherRevenue;
                    break;

            }

            SortExpression expression = null;
            if (sort != null)
            {
                SortOperator sortOperator = sortDirection == SortDirection.Ascending
                                                ? SortOperator.Ascending
                                                : SortOperator.Descending;
                expression = new SortExpression((EntityField)sort | sortOperator);
            }
            else
            {
                expression = new SortExpression
                               {
                                   IssuingOrderFields.ActualShippingDate | SortOperator.Descending
                               };
                expression.Add(IssuingOrderFields.ReceivingOrderDate | SortOperator.Descending);
                expression.Add(RevenueMachineAccountFields.OrderCode | SortOperator.Descending);
            }

            var table = new DataTable("RevenueMachineAccountDS");
            var dao = new TypedListDAO();
            dao.GetMultiAsDataTable(fields, table, 0, expression, GetRevenueMachineAccountPredicate(criteria),
                                    GetRevenueMachineAccountSearchRelations(), false, null, null, pageIndex + 1, pageSize);


            return table;
        }

        public bool AddRevenueMachineAccount(Transaction trans, RevenueMachineAccountData entity)
        {

            var machineAccountEntity = new RevenueMachineAccountEntity();
            GetRevenueMachineAccountEntity(machineAccountEntity, entity);
            machineAccountEntity.Id = entity.Id;
            trans.Add(machineAccountEntity);
            machineAccountEntity.Save();
            return true;

        }

        public bool ModifyRevenueMachineAccount(RevenueMachineAccountData entity)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "ModifyRevenueMachineAccounts");
            try
            {
                ModifyRevenueMachineAccount(trans, entity);
                trans.Commit();

                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }
        private bool ModifyRevenueMachineAccount(Transaction trans, RevenueMachineAccountData entity)
        {

            var machineAccountEntity = new RevenueMachineAccountEntity(entity.Id);
            GetRevenueMachineAccountEntity(machineAccountEntity, entity);
            trans.Add(machineAccountEntity);
            machineAccountEntity.Save();
            return true;

        }

        public bool RemoveRevenueMachineAccount(Guid revenueMachineAccountId)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "RemoveMachineAccounts");
            try
            {

                var machineAccountEntity = new RevenueMachineAccountEntity(revenueMachineAccountId);
                trans.Add(machineAccountEntity);
                machineAccountEntity.Delete();


                trans.Commit();
                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }

        public bool BatchModifyRevenueMachineAccounts(IList<RevenueMachineAccountData> revenueMachineAccounts)
        {
            var trans = new Transaction(IsolationLevel.ReadCommitted, "BatchModifyRevenueMachineAccounts");
            try
            {
                foreach (var revenueMachineAccount in revenueMachineAccounts)
                {
                    ModifyRevenueMachineAccount(trans, revenueMachineAccount);
                }

                trans.Commit();

                return true;
            }
            catch (Exception)
            {
                trans.Rollback();
                throw;
            }
        }



        public RevenueMachineAccountData GetRevenueMachineAccount(Guid revenueMachineAccountId, bool isSimpleView)
        {
            var meta = new LinqMetaData();
            var data = (from revenueMachineAccount in meta.RevenueMachineAccount
                        where revenueMachineAccount.Id == revenueMachineAccountId
                        select revenueMachineAccount).FirstOrDefault();
            if (data == null) return null;
            return GetRevenueMachineAccountData(data, isSimpleView);

        }

        public RevenueMachineAccountData GetRevenueMachineAccount(Guid issuingOrderId)
        {
            var meta = new LinqMetaData();
            var data = (from revenueMachineAccount in meta.RevenueMachineAccount
                        where revenueMachineAccount.IssuingOrderId == issuingOrderId
                        select revenueMachineAccount).FirstOrDefault();
            if (data == null) return null;
            return GetRevenueMachineAccountData(data, true);

        }


        public bool ExistsRevenueMachineAccount(Guid issuingOrderDeliveryId)
        {
            //var meta = new LinqMetaData();
            //return meta.RevenueMachineAccount.Any(a =>
            //                                           a.IssuingOrderDeliveryId == issuingOrderDeliveryId
            //     );
            return true;
        }


        public bool ExistsRevenueMachineAccountByIssuingOrder(Guid issuingOrderId)
        {
            var meta = new LinqMetaData();
            return meta.RevenueMachineAccount.Any(a =>
                                                       a.IssuingOrderId == issuingOrderId
                 );

        }

        #region private methods
        private IPredicate GetRevenueMachineAccountPredicate(RevenueMachineAccountSearchCriteria criteria)
        {
            var expression = new PredicateExpression();

            if (criteria.AreaId.HasValue && criteria.AreaId.Value != Guid.Empty)
            {
                expression.Add(RevenueMachineAccountFields.AreaId == criteria.AreaId);
            }

            if (!string.IsNullOrEmpty(criteria.OrderCode))
            {
                expression.Add(RevenueMachineAccountFields.OrderCode % ("%" + criteria.OrderCode + "%"));
            }
            if (!string.IsNullOrEmpty(criteria.TransportationPlanCode))
            {
                expression.Add(RevenueMachineAccountFields.TransportationPlanCode % ("%" + criteria.TransportationPlanCode + "%"));
            }

            if (criteria.IsStatementofAccountCompleted.HasValue)
            {
                expression.Add(RevenueMachineAccountFields.IsStatementofAccountCompleted == criteria.IsStatementofAccountCompleted);
            }
            if (criteria.IsInvoiced.HasValue)
            {
                expression.Add(RevenueMachineAccountFields.IsInvoiced == criteria.IsInvoiced);
            }
            if (criteria.IsAccountsReceivable.HasValue)
            {
                expression.Add(RevenueMachineAccountFields.IsAccountsReceivable == criteria.IsAccountsReceivable);
            }

            if (criteria.OrderStatus.HasValue)
            {
                expression.Add(IssuingOrderFields.OrderStatus == criteria.OrderStatus);
            }


            if (criteria.ReceiptInfoActualResponseDate != null)
            {
                if (criteria.ReceiptInfoActualResponseDate.Start.HasValue)
                {
                    expression.Add(IssuingOrderReceiptInfoTrackFields.ReceiptInfoActualResponseDate >= criteria.ReceiptInfoActualResponseDate.Start);

                }
                if (criteria.ReceiptInfoActualResponseDate.End.HasValue)
                {
                    expression.Add(IssuingOrderReceiptInfoTrackFields.ReceiptInfoActualResponseDate <= criteria.ReceiptInfoActualResponseDate.End);
                }
            }
            if (criteria.ConfirmAccountsReceiptDate != null)
            {
                if (criteria.ConfirmAccountsReceiptDate.Start.HasValue)
                {
                    expression.Add(RevenueMachineAccountFields.ConfirmAccountsReceiptDate >= criteria.ConfirmAccountsReceiptDate.Start);
                }
                if (criteria.ConfirmAccountsReceiptDate.End.HasValue)
                {
                    expression.Add(RevenueMachineAccountFields.ConfirmAccountsReceiptDate <= criteria.ConfirmAccountsReceiptDate.End);
                }
            }
            if (criteria.ConfirmInvoiceDate != null)
            {
                if (criteria.ConfirmInvoiceDate.Start.HasValue)
                {
                    expression.Add(RevenueMachineAccountFields.ConfirmInvoiceDate >= criteria.ConfirmInvoiceDate.Start);
                }
                if (criteria.ConfirmInvoiceDate.End.HasValue)
                {
                    expression.Add(RevenueMachineAccountFields.ConfirmInvoiceDate <= criteria.ConfirmInvoiceDate.End);
                }
            }

            if (criteria.ConfirmStatementofAccountDate != null)
            {
                if (criteria.ConfirmStatementofAccountDate.Start.HasValue)
                {
                    expression.Add(RevenueMachineAccountFields.ConfirmStatementofAccountDate >= criteria.ConfirmStatementofAccountDate.Start);
                }
                if (criteria.ConfirmStatementofAccountDate.End.HasValue)
                {
                    expression.Add(RevenueMachineAccountFields.ConfirmStatementofAccountDate <= criteria.ConfirmStatementofAccountDate.End);
                }
            }

            if (criteria.ReceivingOrderDate != null)
            {
                if (criteria.ReceivingOrderDate.Start.HasValue)
                {
                    expression.Add(IssuingOrderFields.ReceivingOrderDate >= criteria.ReceivingOrderDate.Start);
                }
                if (criteria.ReceivingOrderDate.End.HasValue)
                {
                    expression.Add(IssuingOrderFields.ReceivingOrderDate <= criteria.ReceivingOrderDate.End);
                }
            }
            if (criteria.ActualShippingDate != null)
            {
                if (criteria.ActualShippingDate.Start.HasValue)
                {
                    expression.Add(IssuingOrderFields.ActualShippingDate >= criteria.ActualShippingDate.Start);
                }
                if (criteria.ActualShippingDate.End.HasValue)
                {
                    expression.Add(IssuingOrderFields.ActualShippingDate <= criteria.ActualShippingDate.End);
                }
            }
            if (criteria.CustomerId.HasValue && criteria.CustomerId.Value != Guid.Empty)
            {
                expression.Add(IssuingOrderFields.CustomerId == criteria.CustomerId);
            }
            if (criteria.RouteTypeId.HasValue && criteria.RouteTypeId.Value != Guid.Empty)
            {
                expression.Add(IssuingOrderFields.RouteTypeId == criteria.RouteTypeId);
            }
            if (criteria.GoodsShippedTypeId.HasValue && criteria.GoodsShippedTypeId.Value != Guid.Empty)
            {
                expression.Add(IssuingOrderFields.GoodsShippedTypeId == criteria.GoodsShippedTypeId);
            }
            return expression;
        }

        private RevenueMachineAccountData GetRevenueMachineAccountData(RevenueMachineAccountEntity revenueMachineAccountEntity, bool isSimpleView)
        {
            var revenueMachineAccountData = new RevenueMachineAccountData()
            {
                Id = revenueMachineAccountEntity.Id,
                OrderCode = revenueMachineAccountEntity.OrderCode,
                AreaId =
                    revenueMachineAccountEntity.AreaId,

                TransportationPlanCode = revenueMachineAccountEntity.TransportationPlanCode,
                IssuingOrderId = revenueMachineAccountEntity.IssuingOrderId,
                TotalCarSharingPrice = revenueMachineAccountEntity.IssuingOrder.IssuingOrderVehicleInfo.Sum(a => a.CarSharingPrice ?? 0),
                CarSharingCostWithFactory = revenueMachineAccountEntity.CarSharingCostWithFactory,
                TotalGoodsUnloadingCost = revenueMachineAccountEntity.IssuingOrder.GoodsUnloadingCost ?? 0,
                Subsidy = revenueMachineAccountEntity.Subsidy,
                OtherRevenue = revenueMachineAccountEntity.OtherRevenue,


                IsStatementofAccountCompleted = revenueMachineAccountEntity.IsStatementofAccountCompleted,
                DueInvoiceDate = revenueMachineAccountEntity.DueInvoiceDate,

                IsInvoiced = revenueMachineAccountEntity.IsInvoiced,
                IsAccountsReceivable = revenueMachineAccountEntity.IsAccountsReceivable,
                AccountsReceivableDate = revenueMachineAccountEntity.AccountsReceivableDate,
                Exceptions = revenueMachineAccountEntity.Exceptions,

                ConfirmAccountsReceiptDate = revenueMachineAccountEntity.ConfirmAccountsReceiptDate,
                ConfirmStatementofAccountDate = revenueMachineAccountEntity.ConfirmStatementofAccountDate,
                ConfirmInvoiceDate = revenueMachineAccountEntity.ConfirmInvoiceDate,

                CreateBy = revenueMachineAccountEntity.CreateBy,
                CreatedDate = revenueMachineAccountEntity.CreatedDate,
                LastUpdatedBy = revenueMachineAccountEntity.LastUpdatedBy,
                LastUpdatedDate = revenueMachineAccountEntity.LastUpdatedDate,

                UnConfirmStatementofAccountComments = revenueMachineAccountEntity.UnConfirmStatementofAccountComments

            };
            var paymentMachineAccount = revenueMachineAccountEntity.IssuingOrder.MachineAccountPayment;

            revenueMachineAccountData.ExtraCost = paymentMachineAccount.Sum(a => a.OtherAmountFromManufactor) -
                                         paymentMachineAccount.Sum(a => a.DeductionExceptionsAmount);


            if (WebConfig.IsCalculateByWeight(revenueMachineAccountEntity.IssuingOrder.GoodsShippedTypeId))
            {

                revenueMachineAccountData.UnloadingCostWithFactory =
                    revenueMachineAccountEntity.IssuingOrder.IssuingOrderDelivery.Sum(a => a.GoodsUnloadingPriceRatio * a.OrderWeight) ?? 0;
                revenueMachineAccountData.ActualUnloadingCostWithFactory =
                    revenueMachineAccountEntity.IssuingOrder.IssuingOrderDelivery.Sum(a => a.GoodsUnloadingPriceRatio * a.ActualEntruckingOrderWeight) ?? 0;


                revenueMachineAccountData.Revenues =
                    revenueMachineAccountEntity.IssuingOrder.IssuingOrderDelivery.Sum(
                        a => a.OrderWeight * a.RevenueUnitCost) ?? 0;
                revenueMachineAccountData.ActualRevenues =
              revenueMachineAccountEntity.IssuingOrder.IssuingOrderDelivery.Sum(
                  a => a.ActualEntruckingOrderWeight * a.RevenueUnitCost) ?? 0;
            }
            else
            {

                revenueMachineAccountData.UnloadingCostWithFactory =
                 revenueMachineAccountEntity.IssuingOrder.IssuingOrderDelivery.Sum(a => a.GoodsUnloadingPriceRatio * a.OrderVolumn) ?? 0;
                revenueMachineAccountData.ActualUnloadingCostWithFactory =
                revenueMachineAccountEntity.IssuingOrder.IssuingOrderDelivery.Sum(a => a.GoodsUnloadingPriceRatio * a.ActualEntruckingOrderVolumn) ?? 0;

                revenueMachineAccountData.Revenues =
                    revenueMachineAccountEntity.IssuingOrder.IssuingOrderDelivery.Sum(
                        a => a.OrderVolumn * a.RevenueUnitCost) ?? 0;
                revenueMachineAccountData.ActualRevenues =
              revenueMachineAccountEntity.IssuingOrder.IssuingOrderDelivery.Sum(
                  a => a.ActualEntruckingOrderVolumn * a.RevenueUnitCost) ?? 0;
            }

            revenueMachineAccountData.TotalRevenues = revenueMachineAccountData.Revenues + revenueMachineAccountEntity.Subsidy + revenueMachineAccountEntity.OtherRevenue + revenueMachineAccountEntity.CarSharingCostWithFactory + revenueMachineAccountData.UnloadingCostWithFactory;
            if (!isSimpleView)
            {

                revenueMachineAccountData.Cost = revenueMachineAccountEntity.IssuingOrder.ShuntingCost + revenueMachineAccountData.TotalCarSharingPrice +
                                        revenueMachineAccountData.TotalGoodsUnloadingCost;

                if (revenueMachineAccountData.TotalRevenues > 0)
                {
                    revenueMachineAccountData.Profit = revenueMachineAccountData.TotalRevenues - revenueMachineAccountData.Cost -
                                     revenueMachineAccountData.ExtraCost;
                    revenueMachineAccountData.ProfitRate = Math.Round(((revenueMachineAccountData.Profit / revenueMachineAccountData.TotalRevenues) * 100), 2);
                }
                else
                {
                    revenueMachineAccountData.ProfitRate = 0;
                }

            }
            return revenueMachineAccountData;
        }
        private RevenueMachineAccountEntity GetRevenueMachineAccountEntity(RevenueMachineAccountEntity revenueMachineAccountEntity, RevenueMachineAccountData revenueMachineAccountData)
        {

            revenueMachineAccountEntity.Id = revenueMachineAccountData.Id;
            revenueMachineAccountEntity.OrderCode = revenueMachineAccountData.OrderCode;
            revenueMachineAccountEntity.AreaId = revenueMachineAccountData.AreaId.Value;
            revenueMachineAccountEntity.TransportationPlanCode = revenueMachineAccountData.TransportationPlanCode;
            revenueMachineAccountEntity.IssuingOrderId = revenueMachineAccountData.IssuingOrderId;

            revenueMachineAccountEntity.OtherRevenue = revenueMachineAccountData.OtherRevenue;

            revenueMachineAccountEntity.CarSharingCostWithFactory = revenueMachineAccountData.CarSharingCostWithFactory;

            revenueMachineAccountEntity.Subsidy = revenueMachineAccountData.Subsidy;

            revenueMachineAccountEntity.IsStatementofAccountCompleted = revenueMachineAccountData.IsStatementofAccountCompleted;
            revenueMachineAccountEntity.DueInvoiceDate = revenueMachineAccountData.DueInvoiceDate;

            revenueMachineAccountEntity.IsInvoiced = revenueMachineAccountData.IsInvoiced;
            revenueMachineAccountEntity.IsAccountsReceivable = revenueMachineAccountData.IsAccountsReceivable;
            revenueMachineAccountEntity.AccountsReceivableDate = revenueMachineAccountData.AccountsReceivableDate;
            revenueMachineAccountEntity.Exceptions = revenueMachineAccountData.Exceptions;

            revenueMachineAccountEntity.ConfirmAccountsReceiptDate = revenueMachineAccountData.ConfirmAccountsReceiptDate;
            revenueMachineAccountEntity.ConfirmStatementofAccountDate = revenueMachineAccountData.ConfirmStatementofAccountDate;
            revenueMachineAccountEntity.ConfirmInvoiceDate = revenueMachineAccountData.ConfirmInvoiceDate;
            revenueMachineAccountEntity.CreateBy = revenueMachineAccountData.CreateBy.Value;
            revenueMachineAccountEntity.CreatedDate = revenueMachineAccountData.CreatedDate.Value;
            revenueMachineAccountEntity.LastUpdatedBy = revenueMachineAccountData.LastUpdatedBy.Value;
            revenueMachineAccountEntity.LastUpdatedDate = revenueMachineAccountData.LastUpdatedDate.Value;

            revenueMachineAccountEntity.UnConfirmStatementofAccountComments =
                revenueMachineAccountData.UnConfirmStatementofAccountComments;

            return revenueMachineAccountEntity;
        }


        #endregion
    }
}
